package com.lee.ssm.service.impl;

import com.lee.ssm.dao.IRoleDao;
import com.lee.ssm.domain.Role;
import com.lee.ssm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("iRoleService")
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleDao iRoleDao;

    @Override
    public List<Role> findAll() throws Exception {
        return iRoleDao.findAll();
    }

    @Override
    public void save(Role role) throws Exception {
        iRoleDao.save(role);
    }

    @Override
    public Role findById(String id) throws Exception {
        return iRoleDao.findById(id);
    }

    @Override
    public void deleteRole(String id) throws Exception {
        iRoleDao.deleteRoleToAllUser(id);
        iRoleDao.deleteRoleToAllPermission(id);
        iRoleDao.deleteRole(id);
    }

    @Override
    public List<Role> findByUserId(String userId) throws Exception {
        return iRoleDao.findRoleByUserId(userId);
    }

    @Override
    public List<Role> findOthersRoleByUserId(String id) throws Exception {
        return iRoleDao.findOthersRoleByUserId(id);
    }

    @Override
    public void addPermissionToRole(String roleId, String[] permissionId) throws Exception {
        for (int i = 0; i < permissionId.length; i++) {
            iRoleDao.addPermissionToRole(roleId,permissionId[i]);
        }
    }
}
